
//************************************************//
// Use the first lottery prize at the treatment year //
//************************************************//

do "E:\yungyu\lottery_baby\do\set_environment.do"

capture log close
log using "$log\2.3.first_event_winners_`c(current_date)'.log" , replace
clear
set more off

foreach x in 2 5 10 30 50 { //Cut at different amount
	dis "Cutoff: `x'K"
	use "$wdata\individual_winners.dta", clear

	gen tot_real_prize_aftertax = pers_real_prize_aftertax + spouse_real_prize_aftertax
	
	dis "--delete prize amount < `x'K"
	keep if pers_real_prize_aftertax >= `x' * 1000 //Only keey prize amount above a given threshold

	dis "--Use first event if with multiple records"
	keep if real_win_year == year
	sort pers_idn year
	duplicates drop pers_idn, force
	
	dis "--Keep winners who win the prize in the treatment year"
	keep if pers_real_prize_aftertax >= `x' * 1000 & real_win_year == year

	dis "--Keep within age 20 to 44"
	qui gen win_age = win_year - pers_born_year
	keep if inrange(win_age,20,44)

	save "$wdata\first_event_winners_cutat`x'k", replace 
}

clear

use "$wdata\first_event_winners_cutat2k", clear
gen first_event_2k = 1
foreach x in 5 10 30 50 { //Cut at different amount
	merge 1:1 real_win_year pers_idn using "$wdata\first_event_winners_cutat`x'k", gen(first_event_`x'k)
	recode first_event_`x'k (2/3=1)(1=0)
}
foreach x in 2 5 10 30 50 { //Cut at different amount
	recode first_event_`x'k . = 0
}

drop if win_year == 102 //Not current and not future winners
save "$wdata\first_event_winners", replace

use "$wdata\single_event_winners", clear
merge 1:1 real_win_year pers_idn using "$wdata\first_event_winners"
drop _m

foreach x in 2 5 10 30 50 { //Cut at different amount
	recode single_event_`x'k . = 0
	recode first_event_`x'k . = 0
	
	lab val single_event_`x'k .
	lab val first_event_`x'k .
}

compress
save "$wdata\single_and_first_event_winners", replace

log close 
